﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Ashlen.Utility.NewsService
{
    /// <summary>
    /// This is the class that represents a virtual news feed service.
    /// This will take in a sourceId and load all rss feeds for that source
    /// and retrieve all news feeds for it.
    /// </summary>
    /// <remarks>
    /// Author: Shailen Sukul
    /// Date: 08 Oct 2008
    /// </remarks>
    public sealed class NewsFeedService : IService
    {
        #region Fields
        private bool _continueLoop = true;
        private int _sourceId = 0;
        private int _waitInterval = 1800000;
        RSSHelper _rssHelper = null;
        #endregion

        #region Constructor
        public NewsFeedService(int sourceId) {
            _rssHelper = new Ashlen.Utility.NewsService.RSSHelper();
            _sourceId = sourceId;
        }

        public NewsFeedService(int sourceId, int? waitInterval)
            : this(sourceId) {
            if (waitInterval.HasValue) {
                _waitInterval = waitInterval.Value;
            }
        }
        #endregion

        public void Initialize(List<string> parameters) { }

        public void Start() {
            try {
                Log.LogEvent(System.Diagnostics.EventLogEntryType.Information, string.Format("Starting service for sourceId: {0}", _sourceId));
                while (_continueLoop) {
                    try {
                        _rssHelper.CrawlFeeds(_sourceId);
                    }
                    catch (ThreadInterruptedException) { }
                    catch (ThreadAbortException) { }
                    catch (Exception ex) {
                        Log.LogEvent(System.Diagnostics.EventLogEntryType.Error,
                            string.Format("Error occurred for source id: {0}{1}{2}", _sourceId, System.Environment.NewLine, ex.ToString()));
                    }
                    Log.LogEvent(System.Diagnostics.EventLogEntryType.Information,
                            string.Format("Sleeping for {0} milliseconds for source {1}.", _waitInterval, _sourceId));
                    Thread.Sleep(new System.TimeSpan(0, 0, (_waitInterval / 1000)));
                    Log.LogEvent(System.Diagnostics.EventLogEntryType.Information,
                        string.Format("Woke up for source {0}", _sourceId));
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex) {
                Log.LogEvent(System.Diagnostics.EventLogEntryType.Error,
                    string.Format("Error occurred for source id: {0}{1}{2}", _sourceId, System.Environment.NewLine, ex.ToString()));
            }
        }

        public void Stop() {
            Log.LogEvent(System.Diagnostics.EventLogEntryType.Information, string.Format("Stopping service for sourceId: {0}", _sourceId));
            _continueLoop = false;
            Thread.CurrentThread.Interrupt();
        }
    }
}
